//Jared Lobberecht (2007.12.21)(http://www.lobberecht.com)
//Modifed from Chris Campbell's Lightbox Lite (http://particletree.com)

function MiniLightbox(ctrlId, overlayId) {
		this.isIE = (navigator.userAgent.toLowerCase().indexOf('msie') > -1);
		this.contentid = ctrlId;
		this.overlayid = overlayId;
}
MiniLightbox.prototype = {
	yPos : 0,
	xPos : 0,
	overlayClassName : 'lightboxoverlay',
	contentClassName : 'lightboxcontent',

	activate: function(){
		if (this.isIE){
			this.getScroll();
			this.prepareIE('100%', 'auto');
			this.setScroll(0,0);
		}
		this.displayLightbox("block");
	},
	
	deactivate: function(){
		if (this.isIE){
			this.setScroll(0,this.yPos);
			this.prepareIE("auto", "auto");
		}
		this.displayLightbox("none");
	},
	
	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
	prepareIE: function(height, overflow){
		var bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		bod.style.overflow = overflow;
  
		var htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow; 
	},
	
	// Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
	getScroll: function(){
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	},
	
	setScroll: function(x, y){
		window.scrollTo(x, y); 
	},
	
	addClassName: function(el, className){
		if(el.className.match(className) == null){
			if(el.className.length > 0) el.className += ' ';
			el.className += className;
		}
	},
	
	defineVariable: function(elid, classname){
		var v = document.getElementById(elid);
		if( v != null ){
		    this.addClassName(v, classname);
		}
		return v;
	},
	
	displayLightbox: function(display){
	    if(this.overlay == undefined){
		    this.overlay = this.defineVariable(this.overlayid, this.overlayClassName);
		}
		this.overlay.style.display = display;

	    if(this.content == undefined){
    		this.content = this.defineVariable(this.contentid, this.contentClassName);
		}
		this.content.style.display = display;
		if(display != 'none'){
		    this.content.style.marginLeft = (-1 * (this.content.offsetWidth / 2)) + 'px';
		    var newhmargin = -1 * ((this.content.offsetHeight / 2) + (this.content.offsetHeight / 6));
		    this.content.style.marginTop = newhmargin + 'px';
		    
		    var top = this.findPosition(this.content)[1];
		    if(top < 0){
		        this.content.style.marginTop = '0px';
		        this.content.style.top = '0px';
		    }
		    
		    //Reset overlay height
		    this.overlay.style.height = this.backgroundHeight() + 'px';
		}
	},
	
	findPosition: function(obj){
	    var curtop = 0;
	    var curleft = 0;
	    if (obj.offsetParent) {
		    curleft = obj.offsetLeft
		    curtop = obj.offsetTop
		    while (obj = obj.offsetParent) {
			    curleft += obj.offsetLeft
			    curtop += obj.offsetTop
		    }
	    }
	    return [curleft, curtop];
    },
    
    backgroundHeight: function() {
        var clientHeight = Math.max(document.documentElement.clientHeight, document.body.clientHeight);
        var scrollHeight = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
        //Return the max between client height and scroll height to account for the popup extending beyond the client height
        return Math.max(scrollHeight, clientHeight);
    }
}
